package com.rem.designPattern.Command命令;

/**
 * 命令模式
 * 将一个请求封装为一个对象，从而使你可用不同的请求对客户进行参数化；对请求排队或记录请求日志，以及支持可撤消的操作
 * <p>
 * 调用者
 *
 * @author Rem
 * @date 2022-11-19
 */
public class RemoteController {
    private final Command[] onCommands;
    private final Command[] offCommands;
    //缓存上一次执行的命令
    private Command restoreCommand;

    /**
     * 初始化全置为空命令
     */
    public RemoteController() {
        onCommands = new Command[5];
        offCommands = new Command[5];
        for (int i = 0; i < 5; i++) {
            onCommands[i] = new NonCommand();
            offCommands[i] = new NonCommand();
        }
    }

    /**
     * 初始化命令
     *
     * @param no
     * @param onCommand
     * @param offCommand
     */
    public void setCommands(int no, Command onCommand, Command offCommand) {
        onCommands[no] = onCommand;
        offCommands[no] = offCommand;
    }

    /**
     * 执行打开的命令
     *
     * @param no
     */
    public void onBtnCommand(int no) {
        restoreCommand = onCommands[no];
        restoreCommand.execute();
    }

    /**
     * 执行关闭的命令
     *
     * @param no
     */
    public void offBtnCommand(int no) {
        restoreCommand = offCommands[no];
        restoreCommand.execute();
    }

    /**
     * 撤销命令
     */
    public void undoBtnCommand() {
        System.out.println("撤销～");
        restoreCommand.undo();
    }
}
